package com.cxm.weilai.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;


/**
 * 统计工具包.
 * @author admin
 *
 */
public class StatisticsUtil {

    /**
     * 将weekList中按照type进行转化，type:0按周；type:1按月；type:2按季度；type:3按年；type:0按天；
     * 按周转化时，获取时间对应的周数，以此周向前统计7周的，weekList中的key对应周数，如果没有要统计的周数，将其num赋0,并添加到weekList的对应位置上
     */
    public static ArrayList<HashMap<String, Object>> transform(ArrayList<HashMap<String, Object>> weekList, String type) {
        HashMap<String, Object> hashMap;
        // 获取转化后的key(当前时间)
        String transformKey;
        // 获取需要比较的key
        // String compareKey;下文没有用到

        for (int i = 0; i < weekList.size(); i++) {
            transformKey = getTransForm(type, i);
            switch (i) {
                case 0:
                    if (!weekList.get(0).get("KEY").equals(transformKey)) {
                        hashMap = new HashMap<String, Object>();
                        hashMap.put("NUM", 0);
                        hashMap.put("KEY", transformKey);
                        weekList.add(0, hashMap);
                    }
                    break;
                case 1:
                    if (!weekList.get(1).get("KEY").equals(transformKey)) {
                        hashMap = new HashMap<String, Object>();
                        hashMap.put("NUM", 0);
                        hashMap.put("KEY", transformKey);
                        weekList.add(1, hashMap);
                    }
                    break;
                case 2:
                    if (!weekList.get(2).get("KEY").equals(transformKey)) {
                        hashMap = new HashMap<String, Object>();
                        hashMap.put("NUM", 0);
                        hashMap.put("KEY", transformKey);
                        weekList.add(2, hashMap);
                    }
                    break;
                case 3:
                    if (!weekList.get(3).get("KEY").equals(transformKey)) {
                        hashMap = new HashMap<String, Object>();
                        hashMap.put("NUM", 0);
                        hashMap.put("KEY", transformKey);
                        weekList.add(3, hashMap);
                    }
                    break;
                case 4:
                    if (!weekList.get(4).get("KEY").equals(transformKey)) {
                        hashMap = new HashMap<String, Object>();
                        hashMap.put("NUM", 0);
                        hashMap.put("KEY", transformKey);
                        weekList.add(4, hashMap);
                    }
                    break;
                case 5:
                    if (!weekList.get(5).get("KEY").equals(transformKey)) {
                        hashMap = new HashMap<String, Object>();
                        hashMap.put("NUM", 0);
                        hashMap.put("KEY", transformKey);
                        weekList.add(5, hashMap);
                    }
                    break;
                case 6:
                    if (!weekList.get(6).get("KEY").equals(transformKey)) {
                        hashMap = new HashMap<String, Object>();
                        hashMap.put("NUM", 0);
                        hashMap.put("KEY", transformKey);
                        weekList.add(6, hashMap);
                    }
                    break;
                default:
                    break;
            }
        }
        return weekList;
    }

    private static String getTransForm(String type, int index) {
        String transformKey = null;
        if ("0".equals(type)) {
            transformKey = DateUtil.getWeekMember(new Date(), index) + "";
        } else if ("1".equals(type)) {
            transformKey = DateUtil.getMonthMember(new Date(), index);
        } else if ("3".equals(type)) {
            transformKey = DateUtil.getYearMember(new Date(), index);
        } else if ("4".equals(type)) {
            transformKey = DateUtil.getDayMember(new Date(), index);
        }
        return transformKey;
    }

    /**
     * 对比.
     */
    public static String compare(ArrayList<HashMap<String, Object>> list, int index) {
        if (list.size() - 1 < index) {
            return "0";
        } else {
            return list.get(index).get("NUM").toString();
        }
    }

}
